import Server from '@/config/server';
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode: false,
  dataLists: [],
  Fields: [],
  templateDataLists: [],
  templateFields: [],
  registerAllData: [],
  itemDetail: [],
  addItemData: {},
  TemplateAllData: [],
};
const actions = {
  // 导出报警列表
  alarmRecord({ commit }, data) {
    const params = JSON.parse(JSON.stringify(data));
    delete params.page;
    return request.get(Server.action.AlarmRecord, { params, responseType: 'blob' });
  },
  // 列表
  getLists({ commit }, page) {
    request.get(Server.action.register_list, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          const listData = response.data.result.list;
          listData.data.forEach((item) => {
            item.key = item.row_num;
          });
          commit('setLists', listData);
          const { fields } = response.data.result;
          fields.push({
            key: 'action',
            title: '操作',
            align: 'center',
            width: 120,
            scopedSlots: { customRender: 'action' },
            fixed: 'right',
          });
          fields.forEach((item, i) => {
            item.dataIndex = item.key;
            item.scopedSlots = { customRender: item.key };
            item.align = 'center';
            if (item.key == 'row_num') item.width = 60;
            if (item.key == 'name') item.width = 220;
            if (item.key == 'unit_name') item.width = 70;
            if (item.key == 'decimal_point') item.width = 70;
            if (item.key == 'type_name') item.width = 101;
            if (item.key == 'plc_type_name') item.width = 133;
            if (item.key == 'address') item.width = 91;
            if (item.key == 'read_write_name') item.width = 90;
            if (item.key == 'data_bits_name') item.width = 84;
            if (item.key == 'number_type_name') item.width = 161;
            if (item.key == 'byte_order_name') item.width = 97;
            if (item.key == 'unitID') item.width = 91;
            if (item.key == 'data_type_name') item.width = 90;
            if (item.key == 'format') item.width = 91;
            if (item.key == 'pay_status_name') item.width = 90;
          });
          commit('setFields', fields);
        } else {

        }
      });
  },
  // 添加
  dataAdd({ commit }, page) {
    commit('setErrorCode', false);
    request.post(Server.action.register_add, page)
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
          commit('setAddItem', response.data.message);
        } else if (response.status === 422) {
          commit('setErrorCode', 422);
          commit('setErrorCode', response.status);
          showErrors(response.data.errors);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 编辑
  dataEdit({ commit }, page) {
    commit('setErrorCode', false);
    request.post(Server.action.register_update, page)
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // ID删除 > 参数为：url/id
  dataDel({ commit }, id) {
    request.get(Server.action.register_delete + id)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 设备寄存器详细信息
  registerDetail({ commit }, id) {
    console.log(id);
    if (!id) {
      commit('setRegisterDetail', []);
      return null;
    }
    request.get(Server.action.register_registerDetail + id)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setRegisterDetail', response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 寄存器所有信息
  registerAll({ commit }) {
    request.get(Server.action.register_all)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setRegisterAll', response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 模板列表
  getTemplateLists({ commit }, page) {
    request.get(Server.action.template_list, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setTemplateLists', response.data.result.list);
          const { fields } = response.data.result;
          fields.push({
            title: '操作',
            align: 'center',
            // width: 200,
            scopedSlots: { customRender: 'action' },
            // fixed: 'right',
          });
          fields.forEach((item, i) => {
            fields[i].dataIndex = fields[i].key;
            fields[i].align = 'center';
            // if(i < fields.length-2) {
            fields[i].width = item.width || 200;
            // }
          });
          commit('setTemplateFields', fields);
        }
      });
  },
  // 模板删除
  templateDel({ commit }, id) {
    request.get(Server.action.template_delete + id)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 寄存器模板添加
  templateAdd({ commit }, fromItem) {
    return request.post(Server.action.template_add, fromItem);
  },
  // 寄存器调用模板
  callTemplate({ commit }, fromItem) {
    return request.post(Server.action.template_callTemplate, fromItem);
  },
  // 所有模板信息
  templateAll({ commit }) {
    request.get(Server.action.template_all)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setTemplateAll', response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  template_callTemplate() {

  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
  },
  setLists(state, lists) {
	    state.dataLists = lists;
  },
  setFields(state, lists) {
	    state.Fields = lists;
  },
  setRegisterDetail(state, data) {
    state.itemDetail = data;
  },
  setRegisterAll(state, data) {
    state.registerAllData = data;
  },
  setAddItem(state, data) {
    state.addItemData = data;
  },
  setTemplateAll(state, data) {
    state.TemplateAllData = data;
  },
  setTemplateLists(state, lists) {
    state.templateDataLists = lists;
  },
  setTemplateFields(state, lists) {
    state.templateFields = lists;
  },


};
export default {
  namespaced: true,
  state,
  actions,
  mutations,
};
